package arduino.comm.connection;

import gnu.io.CommPortIdentifier;

import java.util.Enumeration;

public class RS232Base {

    private Enumeration _portList = null;
    private CommPortIdentifier _portId = null;
    private String _defaultPort = null;
    private boolean _portFound = false;
    private int _baudRate = 0;

    /*******************************
     * Constructor for the base class
     * ******************************/
    public RS232Base(String defaultPort, int baudRate) {
	_defaultPort = defaultPort;
	_baudRate = baudRate;
    }

    /********************************
     * Methode to check the presence of ports on this system
     *******************************/
    public void checkPorts() {

	/***************************************
	 * Get a list of all ports on the system
	 **************************************/
	_portList = CommPortIdentifier.getPortIdentifiers();
	System.out.println("List of all serial ports on this system:");

	while (_portList.hasMoreElements()) {
	    _portId = (CommPortIdentifier) _portList.nextElement();
	    if (_portId.getName().equals(_defaultPort)) {
		_portFound = true;
		System.out.println("Port found on: " + _defaultPort);
		// If port found, create a new class
		SerialValidator serialValidator = new SerialValidator(_portId, _baudRate);
		if (!serialValidator.isArduino()) {

		}
	    }
	}

	if (!_portFound) {
	    System.out.println("No serial port found!!!");
	}
    }

    public static void main(String[] args) {
	RS232Base serial = new RS232Base("/dev/ttyUSB0", 115000);
	serial.checkPorts();
    }

}